package com.aoxon.cargo.activity;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.MeasureSpec;
import android.view.animation.AnimationUtils;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TabHost;

import com.aoxon.cargo.R;
import com.aoxon.cargo.activity.n.IndexActivity;
import com.aoxon.cargo.state.MyStateUtil;

/**
 * 下标签导航栏
 * 
 * @author ZZJ
 */
@SuppressWarnings("deprecation")
public class CargoActivity extends TabActivity implements OnCheckedChangeListener {

    public static int         OPEN_CHOOSE     = MyStateUtil.OPEN_INDEX;

    private TabHost           myTabHost;

    private Intent            indexIntent;
    private Intent            supplierIntent;
    private Intent            collectionIntent;
    private Intent            moreIntent;

    private RadioButton       rbIndex;
    private RadioButton       rbSupplier;
    private RadioButton       rbCollection;
    private RadioButton       rbMore;

    //public LocationClient     mLocationClient = null;
    //public BDLocationListener myListener      = new MyLocationListener();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pur_bottom_tab);

        // this.indexIntent = new Intent(this, PurIndexActivity.class);
        this.indexIntent = new Intent(this, IndexActivity.class);
        this.supplierIntent = new Intent(this, PurSupplierListActivity.class);
        this.collectionIntent = new Intent(this, PurCollectionActivity.class);
        this.moreIntent = new Intent(this, PurMoreActivity.class);

        rbIndex = (RadioButton) findViewById(R.id.rbPurBottomTabToIndex);
        rbSupplier = (RadioButton) findViewById(R.id.rbPurBottomTabToSupplier);
        rbCollection = (RadioButton) findViewById(R.id.rbPurBottomTabToCollection);
        rbMore = (RadioButton) findViewById(R.id.rbPurBottomTabToMore);

        rbIndex.setOnCheckedChangeListener(this);
        rbSupplier.setOnCheckedChangeListener(this);
        rbCollection.setOnCheckedChangeListener(this);
        rbMore.setOnCheckedChangeListener(this);

        setupIntent();

        open();
        // 定位
      //  initLocal();
    }

    private void open() {
        switch (OPEN_CHOOSE) {
            case MyStateUtil.OPEN_INDEX:
                rbIndex.setChecked(true);
                break;
            case MyStateUtil.OPEN_SUPPLIER:
                rbSupplier.setChecked(true);
                break;
            case MyStateUtil.OPEN_COLLECTED:
                rbCollection.setChecked(true);
                break;
            case MyStateUtil.OPEN_MORE:
                rbMore.setChecked(true);
                break;

            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        buttonView.setTextColor(this.getResources().getColor(R.color.assist_gray_font));
        int currentTab = myTabHost.getCurrentTab();
        if (isChecked) {
            switch (buttonView.getId()) {
                case R.id.rbPurBottomTabToIndex:
                    rbIndex.setTextColor(this.getResources().getColor(R.color.blue));
                    setCurrentTabWithAnim(currentTab, 0, "INDEX");
                    break;
                case R.id.rbPurBottomTabToSupplier:
                    rbSupplier.setTextColor(this.getResources().getColor(R.color.blue));
                    setCurrentTabWithAnim(currentTab, 1, "SUPPLIER");
                    this.myTabHost.setCurrentTabByTag("SUPPLIER");
                    break;
                case R.id.rbPurBottomTabToCollection:
                    rbCollection.setTextColor(this.getResources().getColor(R.color.blue));

                    setCurrentTabWithAnim(currentTab, 2, "COLLECTION");
                    break;
                case R.id.rbPurBottomTabToMore:
                    rbMore.setTextColor(this.getResources().getColor(R.color.blue));
                    setCurrentTabWithAnim(currentTab, 3, "MORE");
                    break;
                default:
                    break;
            }
        }
    }

    private void setCurrentTabWithAnim(int now, int next, String tag) {
        // 这个方法是关键，用来判断动画滑动的方向

        if (now > next) {
            myTabHost.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right_out));
            myTabHost.setCurrentTabByTag(tag);
            myTabHost.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right_in));
        } else if (now < next) {
            myTabHost.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left_out));
            myTabHost.setCurrentTabByTag(tag);
            myTabHost.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left_in));
        } else myTabHost.setCurrentTabByTag(tag);
    }

    private void setupIntent() {
        this.myTabHost = getTabHost();
        TabHost localTabHost = this.myTabHost;

        // localTabHost.addTab(buildTabSpec("INDEX", R.string.strHome,
        // R.drawable.home,
        // this.indexIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
        // 切换时保留原来状态
        localTabHost.addTab(buildTabSpec("INDEX", R.string.strHome, R.drawable.home, this.indexIntent));
        // localTabHost.addTab(buildTabSpec("SUPPLIER", R.string.strSupplier,
        // R.drawable.supplier,
        // this.supplierIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
        localTabHost.addTab(buildTabSpec("SUPPLIER", R.string.strSupplier, R.drawable.supplier, this.supplierIntent));
        localTabHost.addTab(buildTabSpec("COLLECTION", R.string.strCollection, R.drawable.collection,
                                         this.collectionIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
        localTabHost.addTab(buildTabSpec("MORE", R.string.strMore, R.drawable.more,
                                         this.moreIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

    }

    private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon, final Intent content) {
        return this.myTabHost.newTabSpec(tag).setIndicator(getString(resLabel)).setContent(content);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.exit:
                this.finish();
                System.exit(0);
                break;
            default:
                break;
        }
        return true;
    }

    // -----------------------------------------------------------------
/*    private void initLocal() {
        mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
        mLocationClient.registerLocationListener(myListener); // 注册监听函数
        //
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);
        option.setAddrType("all");// 返回的定位结果包含地址信息
        // option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
        option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
        option.disableCache(false);// 禁止启用缓存定位
        option.setPoiNumber(5); // 最多返回POI个数
        option.setPoiDistance(1000); // poi查询距离
        option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息
        mLocationClient.setLocOption(option);
        mLocationClient.start();
        // // 发起定位请求
        // if (mLocationClient != null && mLocationClient.isStarted())
        // mLocationClient.requestLocation();
        // else
        // Log.d("LocSDK3", "locClient is null or not started");
    }

    public class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location == null) return;
            StringBuffer sb = new StringBuffer(256);
            sb.append("time : ");
            sb.append(location.getTime());
            sb.append("\nerror code : ");
            sb.append(location.getLocType());
            sb.append("\nlatitude : ");
            sb.append(location.getLatitude());
            sb.append("\nlontitude : ");
            sb.append(location.getLongitude());
            sb.append("\nradius : ");
            sb.append(location.getRadius());
            if (location.getLocType() == BDLocation.TypeGpsLocation) {
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
            }
            Log.i("loacal", "---------------------:" + sb.toString() + ",city:" + location.getCity());
            // 获取一次后停止
            mLocationClient.stop();
        }

        public void onReceivePoi(BDLocation poiLocation) {
            if (poiLocation == null) {
                return;
            }
            StringBuffer sb = new StringBuffer(256);
            sb.append("Poi time : ");
            sb.append(poiLocation.getTime());
            sb.append("\nerror code : ");
            sb.append(poiLocation.getLocType());
            sb.append("\nlatitude : ");
            sb.append(poiLocation.getLatitude());
            sb.append("\nlontitude : ");
            sb.append(poiLocation.getLongitude());
            sb.append("\nradius : ");
            sb.append(poiLocation.getRadius());
            if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
                sb.append("\naddr : ");
                sb.append(poiLocation.getAddrStr());
            }
            if (poiLocation.hasPoi()) {
                sb.append("\nPoi:");
                sb.append(poiLocation.getPoi());
            } else {
                sb.append("noPoi information");
            }
            Log.i("loacal", "**********************************" + sb.toString() + ",city:" + poiLocation.getCity());
        }
    }
*/
}

